﻿#include "precomp.h"
#include "MainMenuDisplay.h"



MainMenuDisplay::MainMenuDisplay(CL_GUIManager *manager, CL_GUITopLevelDescription desc) :
	CL_GUIComponent(manager,desc) 
{
	
//-----Init child components--------------------------------------------------------------

	// adventure button
	CL_PushButton *adventureButton = new CL_PushButton(this);
	advButton = adventureButton;
	advButton->set_geometry(CL_Rect(330,200,630,330));

	// adventure display
	AdventureDisplay *adventureDisplay = new AdventureDisplay(this);
	advDisplay = adventureDisplay;

	// main menu button
	CL_PushButton *mmButton = new CL_PushButton(this);
	mainMenuButton = mmButton;

	
	
//-----------------------------------------------------------------------------------------------


//-----Init main menu display attributes-----------------------------------------------
	quit = false;
	stopRender = false;
//-----------------------------------------------------------------------------------------------


//-----Connect to event functions----------------------------------------------------------------

	// Connect to main menu's event functions
	func_render().set(this,&MainMenuDisplay::on_render);
	func_process_message().set(this, &MainMenuDisplay::on_process_message);
	func_close().set(this,&MainMenuDisplay::on_window_close,this);
	func_input().set(this,&MainMenuDisplay::on_input,this);

	// Connect to button's event functions
	adventureButton->func_clicked().set(this,&MainMenuDisplay::on_clicked,BUTTON_ADVENTURE);
	mainMenuButton->func_clicked().set(this,&MainMenuDisplay::on_clicked,BUTTON_MAINMENU);
//-----------------------------------------------------------------------------------------------

//----Testing area----------------------------------------------------------------------------
	
	CL_GraphicContext gc = get_gc();
	CL_PixelBuffer mainMenuImage = CL_ImageProviderFactory::load("Resource\\game-menu-screen.png");
	CL_Texture *tt  = new CL_Texture(gc,cl_texture_2d);
	mainMenuTexture = tt;
	mainMenuTexture->set_image(mainMenuImage);
	

//-----------------------------------------------------------------------------------------------	
	lastTime = CL_System::get_time();
	manager->exec();// default loop

}

MainMenuDisplay::~MainMenuDisplay(void)
{
	delete advButton;
	delete mainMenuButton;
	delete advDisplay;
	delete mainMenuTexture;
}

bool MainMenuDisplay::on_input(const CL_InputEvent &key, MainMenuDisplay *wnd)
{
	if(key.id == CL_KEY_ESCAPE ) 
	{
		if(!stopRender) 
		{
			quit = true;
			wnd->exit_with_code(0);
			return true;
		}
	}
}
bool MainMenuDisplay::on_window_close( MainMenuDisplay *wnd)
{
	quit = true;
	exit_with_code(0); 
	return true;
}

void MainMenuDisplay::on_clicked(BUTTON_TYPE buttonType) 
{
	switch(buttonType)
	{
	case BUTTON_ADVENTURE:
		{
			stopRender = true;
			advDisplay->set_geometry(get_gc().get_size()); // set working area for advDisplay
			mainMenuButton->set_geometry( CL_Rect(100,100,150,125)); // set working area for mainMenuButton
		}
		break;
	case BUTTON_MAINMENU :
		{
			stopRender = false;
			advDisplay->set_geometry( CL_Rect(0,0,0,0)); // take out working area of advDisplay
			mainMenuButton->set_geometry( CL_Rect(0,0,0,0)); // take out working area of mainMenuButton
		}
		break;
	}
	
}

void MainMenuDisplay::on_render(CL_GraphicContext &gc, const CL_Rect &clip_rect)
{
	if(!quit) // need to against to "request_repaint" call "on_render"
	{
		//---------------------------------CODE HERE-----------------------------------------------------
		unsigned int currentTime = CL_System::get_time();
		float timeDeltaMS = (float) (currentTime - lastTime);
		lastTime = currentTime;
		if(!stopRender)
		{
			CL_Rect client(CL_Point(0,0), get_geometry().get_size());
			CL_Draw::fill(gc, client, CL_Colorf::orange);
			CL_Draw::texture(gc,*mainMenuTexture,CL_Quadf(CL_Rectf(get_geometry().get_size())));
		}
		//-----------------------------------------------------------------------------------------------

		//---Sleep to give other processes more time-----------------------------------------------------
		currentTime = CL_System::get_time();
		const int mainLoopRate = 10;	// 10 ms (100 hz)
		int timeToSleep = mainLoopRate - (currentTime - lastTime);
		if (timeToSleep > 0)
		{
			// Depending on the application
			CL_System::sleep(timeToSleep);
		}
		//------------------------------------------------------------------------------------------------
		request_repaint(); // call on_render again
	}
	else
	{
		// after leave display
		CL_Rect client(CL_Point(0,0), get_geometry().get_size());
		CL_Draw::fill(gc, client, CL_Colorf::black);
	}
	
	
}

void MainMenuDisplay::on_process_message(CL_GUIMessage &message) 
{
	if (!message.is_type(CL_GUIMessage_Close::get_type_name()))
	{
		CL_GUIMessage_Pointer msg_pointer = message;
		switch (msg_pointer.get_pointer_type())
		{
		case CL_GUIMessage_Pointer::pointer_enter:
			
			break;
		case CL_GUIMessage_Pointer::pointer_leave:
			
			break;
		}
		// Cause repaint of component:
		request_repaint();
	}
}

